<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="id" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房型名" prop="roomName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入房型名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间编号" prop="roomNo">
        <el-input
          v-model="queryParams.roomNo"
          placeholder="请输入房间编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="面积" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最底层" prop="floor">
        <el-input
          v-model="queryParams.floor"
          placeholder="请输入最低层数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最高层" prop="maxFloor">
        <el-input
          v-model="queryParams.maxFloor"
          placeholder="请输入最高层数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上次装修时间" prop="lastRenovation">
        <el-date-picker clearable
          v-model="queryParams.lastRenovation"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择装修的时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="开启状态" prop="isDelete">
        <el-switch active-text="开启"
                   inactive-text="关闭"
                   :active-value="0"
                   :inactive-value="1" v-model="queryParams.isDelete" ></el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"

        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"

        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"

        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"

        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="roomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="房型名" align="center" prop="roomName" />
      <el-table-column label="房间编号" align="center" prop="roomNo" />
      <el-table-column label="房间价格" align="center" prop="price" />
      <el-table-column label="房间面积" align="center" prop="area" />
      <el-table-column label="最小层数" align="center" prop="floor" />
      <el-table-column label="最大层数" align="center" prop="maxFloor" />
      <el-table-column label="床id" align="center" prop="bedId" />
      <el-table-column label="是否有独立卫生间" align="center" >
        <template slot-scope="scope">
          {{scope.row.bathroom===1?'有':'无'}}
        </template>
      </el-table-column>
      <el-table-column label="是否有公共厨房" align="center" >
        <template slot-scope="scope">
          {{scope.row.publicKitchen===1?'有':'无'}}
        </template>
      </el-table-column>
      <el-table-column label="是否有私人厨房" align="center" prop="privateKitchen" >
        <template slot-scope="scope">
          {{scope.row.privateKitchen===1?'有':'无'}}
        </template>
      </el-table-column>
      <el-table-column label="上次装修时间" align="center" prop="lastRenovation" width="180">
        <template slot-scope="scope">
          <span>{{ formatDate(scope.row.lastRenovation )}}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属公寓id" align="center" prop="apartmentId" />
      <el-table-column label="开启状态" align="center" prop="isDelete" width="100px">
        <template slot-scope="scope">
          <el-switch
                     :active-value="0"
                     :inactive-value="1" v-model="scope.row.isDelete" ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150px" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['room:room:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['room:room:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改房间对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="房型名" prop="apartmentId">
          <el-select v-model="form.apartmentId" placeholder="请选择">
            <el-option
                v-for="item in apartmentList"
                :key="item.value"
                :label="item.apartmentName"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房型名" prop="roomName">
          <el-input v-model="form.roomName" placeholder="请输入房型名" />
        </el-form-item>
        <el-form-item label="公寓编号" prop="roomNo">
          <el-input v-model="form.roomNo" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="面积" prop="area">
          <el-input v-model="form.area" placeholder="请输入面积" />
        </el-form-item>
        <el-form-item label="最小楼层" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入最小楼层" />
        </el-form-item>
        <el-form-item label="最大楼层" prop="maxFloor">
          <el-input v-model="form.maxFloor" placeholder="请输入最大楼层" />
        </el-form-item>
        <el-form-item label="床形" prop="publicKitchen">
          <el-select v-model="form.bedId" placeholder="请选择">
            <el-option
                v-for="item in bedList"
                :key="item.value"
                :label="item.bedName"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="独立浴室" prop="bathroom">
          <el-checkbox v-model="form.bathroom" :true-label="1" :false-label="0"></el-checkbox>
        </el-form-item>
        <el-form-item label="公共厨房" prop="publicKitchen">
          <el-checkbox v-model="form.publicKitchen" :true-label="1" :false-label="0"></el-checkbox>
        </el-form-item>
        <el-form-item label="私人厨房" prop="privateKitchen">
          <el-checkbox v-model="form.privateKitchen" :true-label="1" :false-label="0"></el-checkbox>
        </el-form-item>
        <el-form-item label="房间设施" prop="stuff">
          <el-select v-model="form.stuffList" multiple placeholder="请选择">
            <el-option
                v-for="item in stuffList"
                :key="item.value"
                :label="item.stuffName"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上次时间" prop="lastRenovation">
          <el-date-picker
              v-model="form.lastRenovation"
              type="date"
              placeholder="选择日期"
              :picker-options="pickerOptions"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上传房间照片" prop="image" >
        <FileUpload :on-remove="handleRemoveImage" :file-list="form.imageList"  @success="handleUploadSuccess">

        </FileUpload>
      </el-form-item>
        <el-form-item label="开启状态" prop="isDelete">
          <el-switch active-text="开启"
                     inactive-text="关闭"
                     :active-value="0"
                     :inactive-value="1" v-model="form.isDelete" ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import { listRoom, getRoom, delRoom, addRoom, updateRoom } from "@/api/room/room";
import Pagination from "@/components/Pagination/index.vue";
import {formatDate} from "../../../../util/DateUtil";
import {listStuff} from "@/api/stuff/stuff";
import FileUpload from "@/components/upload/FileUpload";
import {listBed} from "@/api/bed/bed";
import apartmentApi from "@/api/apartment/apartment";
export default {
  name: "Room",
  components: {Pagination,FileUpload},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房间表格数据
      roomList: [],
      //设施列表数据
      stuffList:[],
      apartmentList:[],
      bedList:[],
      //房间图片数据
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //床查询参数
      queryBedParams: {
        pageNum: 1,
        pageSize: 1000,
        isDelete: 0
      },
      //设施查询参数
      queryStuffParams: {
        pageNum: 1,
        pageSize: 1000,
        id: null,
        roomId: null,
        stuffId: null,
        isDelete: 0
      },
      //只能选择今天以前的日期
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
      // 房间查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        roomName: null,
        roomNo: null,
        price: null,
        area: null,
        floor: null,
        maxFloor: null,
        bedId: null,
        bathroom: null,
        publicKitchen: null,
        privateKitchen: null,
        lastRenovation: null,
        stuffList:[],
        imageList:[],
        apartmentId: null,
        isDelete: 0
      },
      // 表单参数
      form: {
        id: null,
        roomName: null,
        roomNo: null,
        price: null,
        area: null,
        floor: null,
        maxFloor: null,
        bedId: null,
        bathroom: 0,
        publicKitchen: 1,
        privateKitchen: 1,
        lastRenovation: null,
        apartmentId: null,
        isDelete: 0,
        stuffList:[],
        imageList:[]
      },

      // 表单校验
      rules: {
        isDelete: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatDate,
    /** 查询房间列表 */
    getList() {
      // this.form.lastRenovation=this.form.lastRenovation.getTime()
      this.loading = true;
      listRoom(this.queryParams).then(response => {
        this.roomList = response.data.list;
        // this.form.lastRenovation=new Date(this.form.lastRenovation)
        this.total = response.data.total;
        this.loading = false;
        console.log(this.roomList)
      });
      listStuff(this.queryStuffParams).then(
          res=>{
            this.stuffList=res.data.list
          }
      );
      listBed(this.queryBedParams).then(
          res=>{
            console.log("wowowowo")
            this.bedList=res.data.list
          }
      );
      apartmentApi.getList(this.queryBedParams).then(
          res=>{
            this.apartmentList=res.data.list
          }
      )
    },
    handleRemoveImage(file){

      if(file.status === 'success'){
        this.form.imageList=this.form.imageList.filter((image)=>{
          return image.uid===file.uid
        })
      }

    },
    handleUploadSuccess(resp){

      let response=resp.response

      if(response.code === 200){
        this.form.imageList.push({
          uuid:new Date().getTime(),
          name:`图${this.form.imageList.length+1}`,
          url:response.data
        })
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        roomName: null,
        roomNo: null,
        price: null,
        area: null,
        floor: null,
        maxFloor: null,
        bedId: null,
        bathroom: 0,
        publicKitchen: 1,
        privateKitchen: 1,
        lastRenovation: null,
        apartmentId: null,
        isDelete: 0,
        stuffList:[],
        imageList:[]
      };
      let form = this.$refs.form;
      if(form){
        form.resetFields
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加房间";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRoom(id).then(response => {
        this.form = response.data;
        this.form.imageList=response.data.imageList.map((image,index)=>{
          return {
            uid:new Date().getTime()+index,
            name:`图${index+1}`,
            url:image
          }
        })
        this.open = true;
        this.title = "修改房间";
      });
    },
    /** 提交按钮 */
    submitForm() {
      let newForm={
        ...this.form
      }
      console.log(newForm.imageList)
      newForm.imageList=newForm.imageList.map(item=>item.url)
      newForm.stuffList=newForm.stuffList.map(i=>{
        return{
          id:i
        }
      })
      this.$refs["form"].validate(valid => {
        console.log(this.form)
        if (valid) {
          if (this.form.id != null) {
            updateRoom(newForm).then(response => {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRoom(newForm).then(response => {
              this.$message.success("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除房间编号为"' + ids + '"的数据项？').then(function() {
        return delRoom(ids);
      }).then(() => {
        this.getList();
        this.$message.success("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('room/room/export', {
        ...this.queryParams
      }, `room_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
